package com.kcsj.service;

import com.kcsj.dao.NoticeDao;
import com.kcsj.domain.user.Notice;
import com.kcsj.util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
public class NoticeService {
    private static NoticeDao noticeDao = NoticeDao.getInstance();
    private static NoticeService noticeService =new NoticeService();
    private NoticeService(){}
    public static NoticeService getInstance(){
        return noticeService;
    }

    public Collection<Notice> findAllByTypeAndUserId(String type, Integer userId) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Collection<Notice> notices = null;
        if(userId==null){
            notices = noticeDao.findAllByType(type,conn);
        }else {
            notices = noticeDao.findAllByTypeAndUserId(type,userId, conn);
        }
        JdbcHelper.close(conn);
        return notices;
    }

    //Fate Grand Notice?
    public void add(Notice notice) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        if(notice.getUser()!=null){
            noticeDao.add(notice,conn);
        }else {
            noticeDao.addNoUser(notice, conn);
        }
        JdbcHelper.close(conn);
    }

    public Notice findNewNotice(String type,Integer total,Integer userId) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        Notice notice = null;
        if(userId==null){
            notice = noticeDao.findNewNotice(type,total,conn);
        }else {
            notice = noticeDao.findNewNotice(type,total,userId, conn);
        }
        JdbcHelper.close(conn);
        return notice;
    }

    public boolean update(List<Notice> notices) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean update = noticeDao.update(notices, conn);
        JdbcHelper.close(conn);
        return update;
    }

    public boolean delete(Integer id) throws SQLException {
        Connection conn = JdbcHelper.getConn();
        boolean delete = noticeDao.delete(id, conn);
        JdbcHelper.close(conn);
        return delete;
    }
}
